//
//  CZEmotionTipView.swift
//  斯威夫特微博
//
//  Created by youyoumacmini3 on 2017/9/4.
//  Copyright © 2017年 youyoumacmini3. All rights reserved.
//

import UIKit
import pop

/// 表情选择提示图
class CZEmotionTipView: UIImageView {

    // 之前选择的表情
    private var preEmotion: CZEmotion?
    
    /// 提示视图的表情模型
    var emotion: CZEmotion? {
        didSet{
            //
            if emotion == preEmotion {
                return
            }
            
            // 记录当前表情
            preEmotion = emotion
            
            // 表情数据设置
            tipButton.setTitle(emotion?.emoji, for: [])
            tipButton.setImage(emotion?.image, for: [])
            
            // 表情动画 - 弹力动画结束时间根据速度自动计算，不需要也不能指定 duration
            let anim: POPSpringAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
            
            anim.fromValue = 40
            anim.toValue = 8
            anim.springBounciness = 10
            anim.springSpeed = 20
            
            tipButton.layer.pop_add(anim, forKey: nil)
            
            print("设置表情")
        }
    }
    
    
    // MARK: - 私有控件
    private lazy var tipButton = UIButton()
    
    
    // MARK: - 构造函数
    init() {
        let bundle = CZEmotionManager.shared.bundle
        let image = UIImage(named: "emoticon_keyboard_magnifier", in: bundle, compatibleWith: nil)
        
        // [UIImageView alloc] initWithImage: image] -> 会根据图像大小设置图像视图大小
        super.init(image: image)
        
        // 设置锚点
        layer.anchorPoint = CGPoint(x: 0.5, y: 1.2)
        
        // 添加按钮
        tipButton.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        tipButton.frame = CGRect(x: 0, y: 8, width: 36, height: 36)
        tipButton.center.x = bounds.width * 0.5
        tipButton.setTitle("😝", for: [])
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)
        
        addSubview(tipButton)
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
